package com.grep.extra.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.grep.base.db.DBUtils;
import com.grep.base.util.DateUtils;
import com.grep.base.util.UUIDGenerator;
import com.grep.extra.dao.BaseDao;
import com.grep.extra.dao.ContentUrlDao;
import com.grep.extra.factory.data.impl.TaskContentUrl;
import com.grep.extra.factory.data.impl.TaskUrl;

public class ContentUrlDAOSQL implements ContentUrlDao {

	private static Logger logger = LoggerFactory.getLogger(ContentUrlDAOSQL.class);
	private BaseDao baseDao = new BaseDaoImpl();

	public boolean insertContentUrl(String sql) {
		return baseDao.insert(sql);
	}

	public boolean insertContentUrl(TaskContentUrl contentUrl) {
		if (contentUrl != null) {
			String values = " values( ";
			StringBuffer sql = new StringBuffer(" insert into content_urls(id ");
			// 生成id并拼接ID的插入sql
			if (contentUrl.getId() == null) {
				contentUrl.setId(UUIDGenerator.getUUID());
			}
			values = values + "'" + contentUrl.getId() + "'";
			// 生成日期 并拼接日期
			if (contentUrl.getTaskCollectDate() == null) {
				contentUrl.setTaskCollectDate(DateUtils.getDateTime());
			}
			sql.append(",task_collect_date");
			values += ",date_format('" + contentUrl.getTaskCollectDate()
					+ "','%Y-%m-%d %k:%i:%s')";

			if (contentUrl.getTaskContentUrl() != null) {
				sql.append(",task_content_url");
				values += ",'" + contentUrl.getTaskContentUrl() + "'";
			}
			if (contentUrl.getTaskContentStatus() != null) {
				sql.append(",task_content_status");
				values += ",'" + contentUrl.getTaskContentStatus() + "'";
			}
			if (contentUrl.getTaskUrlId() != null) {
				sql.append(",task_url_id");
				values += ",'" + contentUrl.getTaskUrlId() + "'";
			}
			sql.append(") " + values + " )");
			return baseDao.insert(sql.toString());
		} else {
			return false;
		}
	}

	public List<TaskContentUrl>  getContentUrlByUrl(String url) {
		String sql = " select * from content_urls where task_content_url= '"+url+"'";
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<TaskContentUrl> list = new ArrayList<TaskContentUrl>();
		try {
			conn = DBUtils.getConnection();
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery(sql);
			while (rs.next()) {
				TaskContentUrl contentUrl= new TaskContentUrl();
				contentUrl.setId(rs.getString("id"));
				contentUrl.setTaskUrlId(rs.getString("task_url_id")) ;
				contentUrl.setTaskContentUrl(rs.getString("task_content_url")) ;
				contentUrl.setTaskCollectDate(DateUtils.formatDateTime(rs.getTimestamp("task_collect_date"))) ;
				contentUrl.setTaskContentStatus(rs.getString("task_content_status")) ;
				list.add(contentUrl);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error("there have a exception when query data from content_urls");
		} finally {
			DBUtils.close(rs);
			DBUtils.close(pstmt);
			DBUtils.close(conn);
		}
		return list;
	}

}
